﻿using System;
using System.Collections.Generic;
using System.Text;
using ColdSun.Commons.Entities;
using Microsoft.Office.Interop.Excel;
using ColdSun.Commons;
using System.IO;
using ColdSun.Commons.Configs;

namespace ColdSun.Services
{
    public class DepartmentService
    {
        private static DepartmentService serviceInstance = null;
        private List<Department> deparmentList = null;

        public static DepartmentService GetInstance()
        {
            if (serviceInstance == null)
            {
                serviceInstance = new DepartmentService();
            }
            return serviceInstance;
        }

        public DepartmentService()
        {
            Workbook departmentWorkbook = null;
            Worksheet departmentWorksheet = null;
            Microsoft.Office.Interop.Excel.Application excelApplication = null;
            try
            {
                string file = Utilities.GetFullFileName(CommonConfigs.DepartmentFile);
                if (!File.Exists(file))
                {
                    return;
                }

                excelApplication = new Microsoft.Office.Interop.Excel.Application();
                departmentWorkbook = excelApplication.Workbooks.Open(file);
                departmentWorksheet = (Worksheet)departmentWorkbook.Worksheets.get_Item(1);

                int rowIndex = 2;
                deparmentList = new List<Department>();
                while (true)
                {
                    string departmentCode = string.Format("{0}", departmentWorksheet.get_Range(Utilities.Cell("A", rowIndex)).Value);
                    if (string.IsNullOrEmpty(departmentCode))
                    {
                        break;
                    }

                    Department department = new Department();
                    department.DepartmentCode = departmentCode;
                    department.DepartmentFullCode = string.Format("{0}", departmentWorksheet.get_Range(Utilities.Cell("B", rowIndex)).Value);
                    department.DepartmentName = string.Format("{0}", departmentWorksheet.get_Range(Utilities.Cell("C", rowIndex)).Value);
                    department.DepartmentFullName = string.Format("{0}", departmentWorksheet.get_Range(Utilities.Cell("D", rowIndex)).Value);
                    department.DepartmentPlace = string.Format("{0}", departmentWorksheet.get_Range(Utilities.Cell("E", rowIndex)).Value);
                    department.InsuranceAAA = ConvertToBool(departmentWorksheet.get_Range(Utilities.Cell("F", rowIndex)).Value);
                    department.InsuranceToanCau = ConvertToBool(departmentWorksheet.get_Range(Utilities.Cell("G", rowIndex)).Value);
                    department.InsurancePVI = ConvertToBool(departmentWorksheet.get_Range(Utilities.Cell("H", rowIndex)).Value);
                    department.InsuranceBaoMinh = ConvertToBool(departmentWorksheet.get_Range(Utilities.Cell("I", rowIndex)).Value);
                    department.ContractCode = string.Format("{0}", departmentWorksheet.get_Range(Utilities.Cell("J", rowIndex)).Value);
                    deparmentList.Add(department);
                    rowIndex++;
                }
            }
            catch (Exception ex)
            {
                return;
            }
            finally
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                ReleaseComObjectService.ReleaseComObject(departmentWorksheet);
                ReleaseComObjectService.ReleaseWorkbookObject(departmentWorkbook);
                ReleaseComObjectService.ReleaseExcelApplicationObject(excelApplication);
            }
        }

        public List<Department> GetDepartments(string departmentCode)
        {
            if (this.deparmentList == null)
            {
                return null;
            }
            return deparmentList.FindAll(x => x.DepartmentCode == departmentCode);
        }

        public Department GetDepartment(string departmentCode, string contractID)
        {
            if (this.deparmentList == null)
            {
                return null;
            }
            List<Department> departments = GetDepartments(departmentCode);
            return GetDepartment(departments, contractID);
        }

        public Department GetDepartmentByName(string departmentName)
        {
            if (this.deparmentList == null)
            {
                return null;
            }
            return deparmentList.Find(x => x.DepartmentName == departmentName);
        }

        public Department GetDepartment(string departmentCode)
        {
            if (this.deparmentList == null)
            {
                return null;
            }
            return deparmentList.Find(x => x.DepartmentCode == departmentCode);
        }

        public Department GetDepartment(List<Department> departments, string contractID)
        {
            if (departments == null || departments.Count == 0)
            {
                return null;
            }
            if (departments.Count == 1)
            {
                return departments[0];
            }

            //if have more than 1 deparments have the same department code.
            int currentIndex = 0;
            foreach (Department item in departments)
            {
                if (contractID.ToUpper().Trim().Contains(item.ContractCode.Trim().ToUpper()))
                {
                    return item;
                }
                else if (currentIndex == 0) // If the first branch, this one can include the incorrect Contract ID
                {
                    bool isMissingContract = true;
                    foreach (Department checkItem in departments)
                    {
                        if (contractID.ToUpper().Trim().Contains(checkItem.ContractCode.Trim().ToUpper()))
                        {
                            isMissingContract = false;
                            break;
                        }
                    }
                    if (isMissingContract)
                    {
                        return item;
                    }
                }
                currentIndex++;
            }
            return null;
        }

        public Department GetDepartmentInsuranceAAA(string departmentCode)
        {
            if (this.deparmentList == null)
            {
                return null;
            }
            return deparmentList.FindAll(x => x.InsuranceAAA == true).Find(x => x.DepartmentCode == departmentCode);
        }

        public Department GetDepartmentInsuranceToanCau(string departmentCode)
        {
            if (this.deparmentList == null)
            {
                return null;
            }
            return deparmentList.FindAll(x => x.InsuranceToanCau == true).Find(x => x.DepartmentCode == departmentCode);
        }

        public Department GetDepartmentInsurancePVI(string departmentCode)
        {
            if (this.deparmentList == null)
            {
                return null;
            }
            return deparmentList.FindAll(x => x.InsurancePVI == true).Find(x => x.DepartmentCode == departmentCode);
        }

        public Department GetDepartmentInsuranceBaoMinh(string departmentCode)
        {
            if (this.deparmentList == null)
            {
                return null;
            }
            return deparmentList.FindAll(x => x.InsuranceBaoMinh == true).Find(x => x.DepartmentCode == departmentCode);
        }
        public bool ConvertToBool(object value)
        {
            try
            {
                return Convert.ToBoolean(value);
            }
            catch { }
            return false;
        }
    }
}
